#! /usr/bin/env python
import numpy as np
import pyfits as pf
import sys
sys.path.append('/usr/remote/user/sispi/jiangang/decam-fermi')
import glob as gl
from DECamCCD_def import *

if len(sys.argv) == 1:
    print 'syntax: '
    print 'For a given set of images: medianImg image1.fits image2.fits ... '
    print 'For all images in the current dir: medianImg all'
    print 'The resulting median image will be named as median.fits'
elif sys.argv[1]=='all':
    ff=gl.glob('*.fits')
    nimg=len(ff)
    hdu=pf.open(ff[0])
    hdu.verify('silentfix')
    nExt=len(hdu)
    for i in range(1,nExt):
        print i
        for j in range(0,nimg):
            b=[]
            b.append(pf.getdata(ff[j],i))
        hdu[i].data=np.median(b,axis=0)
        hdu[i].header.update('bzero',0)
    hdu.writeto('median.fits')
else:
    nimg=len(sys.argv)-1
    hdu=pf.open(sys.argv[1])
    hdu.verify('silentfix')
    nExt=len(hdu)
    for i in extidx:
        print i
        for j in range(1,nimg+1):
            b=[]
            b.append(pf.getdata(sys.argv[j],i))
        hdu[i].data=np.median(b,axis=0)
        hdu[i].header.update('bzero',0)
    hdu.writeto('median.fits')
